package com.scms.web.controller.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.scms.common.core.controller.BaseController;
import com.scms.common.core.domain.AjaxResult;
import com.scms.common.core.domain.entity.SysMenu;
import com.scms.system.mapper.SysMenuMapper;

import java.util.List;

/**
 * 菜单数据修复控制器
 * 
 * @author scms
 */
@RestController
@RequestMapping("/system/menu/fix")
public class SysMenuFixController extends BaseController
{
    @Autowired
    private SysMenuMapper menuMapper;

    /**
     * 修复菜单父ID为空的问题
     */
    @PreAuthorize("@ss.hasPermi('system:menu:edit')")
    @GetMapping("/parent-id")
    public AjaxResult fixMenuParentId()
    {
        try {
            // 查询所有菜单
            List<SysMenu> menus = menuMapper.selectMenuList(new SysMenu());
            
            int fixedCount = 0;
            // 遍历菜单，修复父ID为空的菜单
            for (SysMenu menu : menus) {
                if (menu.getParentId() == null) {
                    menu.setParentId(0L); // 设置为根菜单
                    menuMapper.updateMenu(menu);
                    fixedCount++;
                }
            }
            
            return AjaxResult.success("成功修复 " + fixedCount + " 个菜单的父ID");
        } catch (Exception e) {
            logger.error("修复菜单父ID时出错", e);
            return AjaxResult.error("修复菜单父ID时出错：" + e.getMessage());
        }
    }
} 